<?
 require_once("functions.php");
 require_once("keywords.php");
 require_once("vendors.php"); 

function addSKU($newSKU_array)
{
	$database = getDB();
	
	$newSKU_treated = arrayEscape($newSKU_array, $database);
	
	$statement = "INSERT INTO Sku (Vendor_id, product_name, weight, description) VALUES ('$newSKU_treated[Vendor_id]', '$newSKU_treated[product_name]', '$newSKU_treated[weight]','$newSKU_treated[description]')";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function blank_sku()
{
	return array('id'=>null, 'created'=>null, 'updated'=>null, 'created_by'=>null, 'updated_by'=>null, 'product_name'=>null, 'description'=>null, 'Vendor_id'=>null, 'weight'=>null);
}

function blank_price()
{
	return array('id'=>null, 'created'=>null, 'updated'=>null, 'created_by'=>null, 'updated_by'=>null, 'price'=>null, 'Sku_id'=>null);
}

function delSKU($idSKU)
{
	$database = getDB();
	
	$statement = "DELETE FROM Sku WHERE id = '$idSKU'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function editSKU($updatearray)
{
	$database = getDB();
	
	$updatearray = arrayEscape($updatearray, $database);

	$statement = "UPDATE Sku SET Vendor_id = '$updatearray[Vendor_id]', product_name = '$updatearray[product_name]', weight = '$updatearray[weight]', description = '$updatearray[description]' WHERE id = '$updatearray[id]'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function getSKU($idSKU)
{
	$database = getDB();
	
	$statement = "SELECT Sku.id AS id, product_name, description, weight, Vendor.id AS vendor_id, Vendor.name AS vendor FROM Sku LEFT JOIN Vendor ON Vendor_id = Vendor.id WHERE Sku.id = '$idSKU'";

	$results = $database->query($statement);

	$sku = $results->fetch_assoc();

	$database->close();
	
	return $sku;
}

function getSKU_all()
{
	$database = getDB();
	
	$statement = "SELECT Sku.id AS id, product_name, description, weight, Vendor.name AS vendor FROM Sku LEFT JOIN Vendor ON Vendor_id = Vendor.id ORDER BY vendor ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

//Price Functions

function addPrice($price_change_array)
{
	$database = getDB();
	
	$price_change_array = arrayEscape($price_change_array, $database);
	
	$statement = "INSERT INTO Price (price, Sku_id, created) VALUES ('$price_change_array[price]', '$price_change_array[Sku_id]', NOW())";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
	
}

function getCurrentPrice($sku)
{
	$database = getDB();
	
	$statement = "SELECT price FROM Price WHERE Sku_id = '$sku' ORDER BY created DESC LIMIT 1";

	$results = $database->query($statement);

	$database->close();
	
	$price = $results->fetch_assoc();
	
	return $price['price'];
	
}

function priceHistory($sku)
{
	$database = getDB();
	
	$statement = "SELECT price, created AS date FROM Price WHERE Sku_id = '$sku' ORDER BY created ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
	
}
?>
